// pages/my/myinfo/myinfo.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    isSyning: false,
    gender: '',
    myinfo: {},
    genderArr: [
      {
        label: '男',
        value: 1,
      },
      {
        label: '女',
        value: 2,
      },
      {
        label: '未知',
        value: '',
      },
    ],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
  },

  onShow:function(options){
    this.getMyInfo();
  },

  /**
  * 获取我的用户信息
  */
  getMyInfo: function () {
    app.Ajax({
      url: '/api/index.php?app=account&act=profile',
      success: (res) => {
        this.setData({
          myinfo: res
        });
        this.setData({
          gender: res.gender
        })
      }
    })
  },

  bindGenderChange: function (e) {
    switch (e.detail.value) {
      case '0': this.setData({ gender: 1 }); break;
      case '1': this.setData({ gender: 2 }); break;
      case '2': this.setData({ gender: 0 }); break;
    }
    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=account&act=edit',
      data: {
        gender: this.data.gender
      },
      success: (res) => {

      },
      fail: (err) => {
        console.log(err);
      }
    })
  },

  linktoAddressList: function () {
    wx.navigateTo({
      url: '../address/address_list/address_list',
    })
  },

  /**
   * 绑定手机
   */
  bindPhone: function () {
    wx.navigateTo({
      url: '../../others/register/register',
    })
  },

  /**
   * 同步微信资料
   */
  synMyinfo: function (e) {
    this.setData({
      isSyning: true
    })
    setTimeout(() => {
      this.setData({
        isSyning: false
      })
    }, 1500)
    var userInfo = e.detail.userInfo;
    app.Ajax({
      method: 'POST',
      url: '/api/index.php?app=account&act=edit',
      data: {
        nick_name: userInfo.nickName,
        portrait: userInfo.avatarUrl,
      },
      success: (res) => {
        this.setData({
          'myinfo.user_name': userInfo.nickName,
          'myinfo.portrait': userInfo.avatarUrl
        })
      }
    })
  }
})